package com.usfuture.util
{

	public class NumberFormat
	{

		private static var _precision:int = 2;
		/**
		 * 保留两位小数
		 * @param value
		 */
		public static function twoPrecision(value:Number):Number
		{
			return Math.round(value*100)/100;;
		}
		
		/**
		 * 保留两位小数
		 * @param value
		 */
		public static function twoPrecisionStr(value:Number):String
		{
			return (Math.round(value*100)/100).toFixed(2);;
		}
		
		/**
		 * 动态保留N位小数
		 * @param value
		 */
		public static function dynamicPrecision(value:Number):Number
		{
			var result:Number;
			switch(_precision){
				case 0:
					result = Math.round(value);
					break;
				case 1:
					result = Math.round(value*10)/10;
					break;
				case 2:
					result = Math.round(value*100)/100;
					break;
				case 3:
					result = Math.round(value*1000)/1000;
					break;
				case 4:
					result = Math.round(value*10000)/10000;
					break;
				default:
					result = Math.round(value*100)/100;
					break;
			}
			return result;
		}
		
		/**
		 * 动态保留N位小数
		 * @param value
		 */
		public static function dynamicPrecisionStr(value:Number):String
		{
			var result:String;
			switch(_precision){
				case 0:
					result = Math.round(value).toFixed(_precision);
					break;
				case 1:
					result =(Math.round(value*10)/10).toFixed(_precision);
					break;
				case 2:
					result = (Math.round(value*100)/100).toFixed(_precision);
					break;
				case 3:
					result = (Math.round(value*1000)/1000).toFixed(_precision);
					break;
				case 4:
					result =(Math.round(value*10000)/10000).toFixed(_precision);
					break;
				default:
					result = (Math.round(value*100)/100).toFixed(_precision);
					break;
			}
			return result;
		}
		
		/**
		 * 前面加0
		 * @param num
		 * @return 
		 */
		public static function preAddZero(num:int):String{
			return num>9?(''+num):('0'+num)
		}

		public static function set precision(value:int):void
		{
			if(value<0 || value>4) return;
			_precision = value;
		}


	}
}